package org.opengauss.datachecker.extract.task;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import java.sql.Types;

import org.opengauss.datachecker.extract.task.functional.CommonTypeHandler;
import org.opengauss.datachecker.extract.task.functional.SqlServerTypeHandler;
import org.opengauss.datachecker.extract.task.functional.SimpleTypeHandler;

/**
 * SqlServerResultSetHandler
 *
 * 处理 SQL Server 结果集，将数据转换为 Types
 */
public class SqlServerResultSetHandler extends ResultSetHandler {
    private final Map<Integer, CommonTypeHandler> commonTypeHandlers = new ConcurrentHashMap<>();
    private final Map<Integer, SimpleTypeHandler> simpleTypeHandlers = new ConcurrentHashMap<>();
    private final Map<String, SqlServerTypeHandler> sqlServerTypeHandlers = new ConcurrentHashMap<>();

    {
        // 处理 SQL Server 数据类型的处理器初始化
        simpleTypeHandlers.put(Types.CHAR, typeHandlerFactory.createCharHandler());
        simpleTypeHandlers.put(Types.INTEGER, typeHandlerFactory.createIntHandler()); // 改为 INTEGER
        simpleTypeHandlers.put(Types.BIGINT, typeHandlerFactory.createLongHandler());
        simpleTypeHandlers.put(Types.VARBINARY, typeHandlerFactory.createBytesHandler());
        
        commonTypeHandlers.put(Types.DATE, typeHandlerFactory.createDateTimeHandler());
        commonTypeHandlers.put(Types.FLOAT, typeHandlerFactory.createFloatHandler());
        commonTypeHandlers.put(Types.DECIMAL, typeHandlerFactory.createBigDecimalHandler());

        // 使用适当的字符串键表示 SQL Server 数据类型
        sqlServerTypeHandlers.put("REAL", typeHandlerFactory.createSqlServerRealHandler());
        sqlServerTypeHandlers.put("TIMESTAMP", typeHandlerFactory.createSqlServerTimestampHandler());
    }

    @Override
    protected String convert(ResultSet resultSet, int columnIdx, ResultSetMetaData rsmd) throws SQLException {
        String columnLabel = rsmd.getColumnLabel(columnIdx);
        String columnTypeName = rsmd.getColumnTypeName(columnIdx);

        if (simpleTypeHandlers.containsKey(columnTypeName)) {
            return simpleTypeHandlers.get(columnTypeName)
                                    .convert(resultSet, columnLabel);
        } else if (commonTypeHandlers.containsKey(columnTypeName)) {
            return commonTypeHandlers.get(columnTypeName)
                                    .convert(resultSet, columnIdx, rsmd);
        } else if (sqlServerTypeHandlers.containsKey(columnTypeName)) {
            // 获取 Field 对象的逻辑
            com.mysql.cj.result.Field field = getFieldFromResultSet(resultSet, columnIdx);
            return sqlServerTypeHandlers.get(columnTypeName)
                                        .convert(resultSet, columnIdx, field);
        } else {
            return defaultObjectHandler.convert(resultSet, columnLabel);
        }
    }

    // 示例方法：获取 Field 对象的逻辑
    private com.mysql.cj.result.Field getFieldFromResultSet(ResultSet resultSet, int columnIdx) {
        // 实现获取 Field 对象的逻辑
        return null; // 示例中返回 null，实际代码中应提供真实实现
    }
}
